﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace QuizNet
{
    class clsDataLayer
    {

        private string connStr;

        public string ConnStr
        {
            get { return connStr; }
            set { connStr = value; }
        }
        private SqlConnection conn;
        private SqlCommand cmd;
        private SqlDataAdapter adp;
        protected quizdbEntities qdb;
        protected clsDataLayer()
        {
            try
            {
                conn = new SqlConnection(connStr);
                qdb = new quizdbEntities(connStr);
            }
            catch
            {
                MessageBox.Show("Không thể kết nối với CSDL", "Thông báo");
            }
        }
        protected clsDataLayer(String str)
        {
            try
            {
                this.connStr = str;
                conn = new SqlConnection(this.connStr);
                qdb = new quizdbEntities(this.connStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        //---------UPDATE---------//
        protected void updateDataBase()
        {
            qdb.AcceptAllChanges();
        }


        protected DataTable Execute(String sqlStr)
        {
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sqlStr;
            cmd.CommandType = CommandType.Text;
            adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            conn.Close();
            return dt;
        }

        protected DataTable Execute(String sqlStr, CommandType cmdType)
        {
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sqlStr;
            cmd.CommandType = cmdType;
            adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            conn.Close();
            return dt;
        }

        protected DataTable Execute(String sqlStr, CommandType cmdType, params SqlParameter[] param)
        {
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sqlStr;
            cmd.CommandType = cmdType;
            foreach (SqlParameter p in param)
            {
                cmd.Parameters.Add(p);
            }
            adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            conn.Close();
            return dt;
        }

        protected string ExecuteQueryXML(String strSQL)
        {
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = strSQL;
            cmd.CommandType = CommandType.Text;
            adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            conn.Close();
            return ds.GetXml();
        }

        protected string ExecuteQueryXML(String strSQL, CommandType cmdType, params SqlParameter[] param)
        {
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = strSQL;
            cmd.CommandType = cmdType;
            foreach (SqlParameter p in param)
            {
                cmd.Parameters.Add(p);
            }
            adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            conn.Close();
            return ds.GetXml();
        }

        protected bool ExecuteNonQuery(String sqlStr, ref String error)
        {
            bool f = false;
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sqlStr;
            cmd.CommandType = CommandType.Text;
            try
            {
                cmd.ExecuteNonQuery();
                f = true;
            }
            catch (SqlException ex)
            {
                error = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return f;
        }

        protected bool ExecuteNonQuery(String sqlStr, CommandType cmdType, ref string error)
        {
            bool f = false;
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sqlStr;
            cmd.CommandType = cmdType;
            try
            {
                cmd.ExecuteNonQuery();
                f = true;
            }
            catch (SqlException ex)
            {
                error = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return f;
        }

        protected bool ExecuteNonQuery(String sqlStr, CommandType cmdType, ref string error, params SqlParameter[] param)
        {
            bool f = false;
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sqlStr;
            cmd.CommandType = cmdType;
            foreach (SqlParameter p in param)
            {
                cmd.Parameters.Add(p);
            }
            try
            {
                cmd.ExecuteNonQuery();
                f = true;
            }
            catch (SqlException ex)
            {
                error = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return f;
        }

        protected object ExecuteScalar(String sqlStr)
        {
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sqlStr;
            cmd.CommandType = CommandType.Text;
            object obj = cmd.ExecuteScalar();
            conn.Close();
            return obj;
        }

        protected object ExecuteScalar(String sqlStr, CommandType cmdType, params SqlParameter[] param)
        {
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sqlStr;
            cmd.CommandType = cmdType;
            foreach (SqlParameter p in param)
            {
                cmd.Parameters.Add(p);
            }
            object obj = cmd.ExecuteScalar();
            conn.Close();
            return obj;
        }
    }
}
